package com.wiim.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import com.wiim.adapter.LichsuAdapter;
import com.wiim.common.ConstantValue;
import com.wiim.common.Utils;
import com.wiim.handler.CMCmdHandler;
import com.wiim.handler.SHOPCmdHandler;
import com.wiim.interfaceapp.IServiceCommandResult;
import com.wiim.model.TransactionLogData;
import com.wiim.model.UserData;
import com.wiim.nhapnhap.AppGlobal;
import com.wiim.nhapnhap.EgoodApp;
import com.wiim.nhapnhap.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class LichSuFragment extends Fragment implements IServiceCommandResult {

	private TableLayout tblTransaction;
	private Button btnEarned, btnUsed;
	private TextView tvTotalUserFee1;
	private TransactionLogData[] transLst;
	private SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm aaa", Locale.ENGLISH);// HH:mm
	private ListView lvHis;
	private ListView lvHis1;
	private LichsuAdapter adapter;
	ArrayList<TransactionLogData> list = new ArrayList<TransactionLogData>();

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.lichsu_layout, container, false);
		initFeld(view);
		return view;
	}

	private void initFeld(View view) {
		tblTransaction = (TableLayout) view.findViewById(R.id.tblTransaction);
		btnEarned = (Button) view.findViewById(R.id.btnEarned);
		btnUsed = (Button) view.findViewById(R.id.btnUsed);
		btnEarned.setTextColor(Color.WHITE);
		tvTotalUserFee1 = (TextView) view.findViewById(R.id.tvTotalUserFee1);
		lvHis = (ListView) view.findViewById(R.id.lvHis);
		lvHis1 = (ListView) view.findViewById(R.id.lvHis1);

		tvTotalUserFee1.setText(getString(R.string.str_bandangco) + " " + new LoginActivity().getUserModel().getRewardPoints() + " " + getString(R.string.str_diem));

		btnEarned.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnEarned.setTextColor(Color.WHITE);
				btnUsed.setTextColor(Color.BLACK);
				tblTransaction.removeAllViews();
				lvHis.setVisibility(View.VISIBLE);
				lvHis1.setVisibility(View.GONE);
				list.removeAll(list);
				if (transLst != null)
					for (TransactionLogData item : transLst) {
						if (!item.CategoryLogID.equalsIgnoreCase(new TransactionCategoryLogTypeId().EXCHANGE_AWARD)) {
							list.add(item);

							// addrow(item.PartnerName,
							// df.format(Date.parse(item.TransDate)),
							// item.Credit);
						}
					}
				adapter = new LichsuAdapter(getActivity(), list, false);
				lvHis.setAdapter(adapter);
			}
		});

		btnUsed.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnEarned.setTextColor(Color.BLACK);
				btnUsed.setTextColor(Color.WHITE);
				tblTransaction.removeAllViews();
				lvHis.setVisibility(View.GONE);
				lvHis1.setVisibility(View.VISIBLE);
				list.removeAll(list);
				if (transLst != null)
					for (TransactionLogData item : transLst) {
						if (item.CategoryLogID.equalsIgnoreCase(new TransactionCategoryLogTypeId().EXCHANGE_AWARD)) {
							list.add(item);

							// addrow(item.PartnerName,
							// df.format(Date.parse(item.TransDate)),
							// item.Debit);
						}
					}
				adapter = new LichsuAdapter(getActivity(), list, true);
				lvHis1.setAdapter(adapter);
			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();

		getData();
	}

	private void getData() {
		Bundle b = new Bundle();
		b.putString(EgoodApp.REST_OBJECT_PUT, new LoginActivity().getUserModel().UserId);
		AppGlobal.getInstance().startServiceCmd(SHOPCmdHandler.COMMAND_GET_TRANSACTION_HISTORY_LIST, LichSuFragment.this, new SHOPCmdHandler(), b, true);
	}

	// public void addrow(String content, String type, String fee) {
	// int wi = tblTransaction.getWidth();
	// /* Create a new row to be added. */
	// TableRow tr = new TableRow(this);
	// tr.setPadding(0, 1, 0, 0);
	// tr.setWeightSum(10);
	//
	// tr.setLayoutParams(new
	// TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 60));
	// /* Create a Button to be the row-content. */
	// TextView b = new TextView(this);
	// b.setText(content);
	// b.setBackgroundResource(R.color.tblrow);
	// b.setTextColor(Color.BLACK);
	// b.setPadding(7, 0, 0, 0);
	// b.setLines(2);
	// b.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
	// TableRow.LayoutParams l1 = new TableRow.LayoutParams(Double.valueOf(wi *
	// 0.5).intValue(), TableRow.LayoutParams.FILL_PARENT);
	// l1.setMargins(0, 0, 1, 0);
	// b.setLayoutParams(l1);
	//
	// TextView c = new TextView(this);
	// c.setText(type);
	// c.setBackgroundResource(R.color.tblrow);
	// c.setTextColor(Color.BLACK);
	// c.setGravity(Gravity.CENTER);
	// TableRow.LayoutParams l2 = new TableRow.LayoutParams(Double.valueOf(wi *
	// 0.25).intValue(), TableRow.LayoutParams.FILL_PARENT);
	// l2.setMargins(0, 0, 1, 0);
	// c.setLayoutParams(l2);
	//
	// TextView d = new TextView(this);
	// d.setText(fee);
	// d.setBackgroundResource(R.color.tblrow);
	// d.setTextColor(Color.BLACK);
	// d.setGravity(Gravity.CENTER);
	// d.setLayoutParams(new TableRow.LayoutParams(Double.valueOf(wi *
	// 0.25).intValue(), TableRow.LayoutParams.FILL_PARENT));
	//
	// /* Add Button to row. */
	// tr.addView(b);
	// tr.addView(c);
	// tr.addView(d);
	// /* Add row to TableLayout. */
	// // tr.setBackgroundResource(R.drawable.sf_gradient_03);
	// tblTransaction.addView(tr, new
	// TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
	// TableLayout.LayoutParams.WRAP_CONTENT));
	// }

	public void processCommandResult(int commandCode, Object resultData) {
		Bundle bResultData = (Bundle) resultData;
		switch (commandCode) {

		case SHOPCmdHandler.COMMAND_GET_TRANSACTION_HISTORY_LIST: {
			transLst = (TransactionLogData[]) bResultData.getSerializable(EgoodApp.REST_OBJECT_RETURN);

			if (transLst != null)
				for (TransactionLogData item : transLst) {
					if (!item.CategoryLogID.equalsIgnoreCase(new TransactionCategoryLogTypeId().EXCHANGE_AWARD)) {
						// addrow(item.PartnerName,df.format(Date.parse(item.TransDate)),
						// item.Balance);
						list.add(item);

						// addrow(item.PartnerName,
						// df.format(Date.parse(item.TransDate)), item.Credit);
					}
				}
			adapter = new LichsuAdapter(getActivity(), list, false);
			lvHis.setAdapter(adapter);
		}
			break;

		default:
			break;
		}
	}

	public class TransactionCategoryLogTypeId {
		// public String REWARD_BY_BUYING = "1";
		// public String REWARD_BY_WATCHING_ADS = "2";
		// public String USER_ACTIVITY = "3";
		// public String USER_REGISTER = "3.1";
		// public String USER_LOGIN_DAILY = "3.2";
		// public String USER_INVITE_NEW = "3.3";
		// public String USER_LIKE_FACEBOOK = "3.4";
		// public String EXCHANGE_AWARD = "4";
		// public String REWARD_FOR_EXCHANGE_AWARD = "5";
		// public String SUPPLIER_PAYMENT = "6";
		// public String NHAPNHAP_PAYMENT = "7";
		// public String POST_ADVERTISEMENT = "8";
		public String REWARD_BY_BUYING = "1";
		public String REWARD_BY_WATCHING_ADS = "2";
		// public String USER_ACTIVITY = "3";
		public String USER_REGISTER = "3";
		public String USER_LOGIN_DAILY = "4";
		public String USER_INVITE_NEW = "5";
		public String USER_LIKE_FACEBOOK = "6";
		public String EXCHANGE_AWARD = "7";
		public String REWARD_FOR_EXCHANGE_AWARD = "8";
		public String SUPPLIER_PAYMENT = "9";
		public String NHAPNHAP_PAYMENT = "10";
		public String POST_ADVERTISEMENT = "11";

	}
}
